import * as Koa from 'koa'
import {get, post, middlewares} from '../utils/koa-decorts'

const userData = [
    {
        name:'zs',
        password:'1234'
    },
    {
        name:'ww',
        password:'abcd'
    }
]

@middlewares([
    async (ctx:Koa.Context,next) => {
        if(ctx.headers.token){
            await next();
            return
        }
        throw {
            name:'name',
            message:'未登录'
        }
    }
])
export default class User {
    @get('/users')
    public async getUserList(ctx:Koa.Context){
        console.log(ctx.query);
        ctx.body = userData
    }
    @post('/login',{
        middlewares:[
            async (ctx:Koa.Context,next) => {
                let request:any = ctx.request;
                if(request.body.name){
                    await next();
                    return;
                }
                throw {
                    name:'name',
                    message:'name is undefined'
                }
            }
        ]
    })
    public async getPassword(ctx:Koa.Context){
        ctx.body = userData
    }
}